{extend name="../../public/admin"}
{block name="title"}管理员设置{/block}
{block name="css"}
<link rel="stylesheet" href="__PUBLIC__/admin/css/user.index.css">
{/block}
{block name="body"}
<div id="container">
    <div class="alert">
        <a href="javascript:;" class="btn btn-info btn-sm" @click="addData()"><i class="fa fa-plus"></i> 添加</a>
    </div>
    <ul class="nav nav-tabs">
        <template v-for="(key,index) in navList">
            <li :class="{'active':thisNav == index}">
                <a href="javascript:;" @click="upda(index)" :title="key.remark+'创建时间：'+key.create_time">
                    {{key.name}}<i class="fa fa-trash-o" @click.stop="deleteGroup(index)" v-if=""></i>
                </a>

            </li>
        </template>
    </ul>
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr class="active">
                    <th width="10%">编号</th>
                    <th width="10%">账号</th>
                    <th width="30%">创建时间</th>
                    <th width="20%">备注</th>
                    <th width="20%" class="text-center">操作</th>
                </tr>
            </thead>
            <tbody>
            <template v-for="(key,index) in list">
            <tr>
                <td>{{index+1}}</td>
                <td>{{key.account}}</td>
                <td>{{key.create_time}}</td>
                <td>{{key.remark}}</td>
                <td class="text-center">
                    <a href="javascript:;" title="删除" class="btn btn-xs" @click="de(index)"><i class="fa fa-trash-o"></i></a>
                    <a href="javascript:;" title="修改密码" class="btn btn-xs" @click="updatePaw(index)"><i class="fa fa-key"></i></a>
                    <a href="javascript:;" title="设置权限" class="btn btn-xs" @click="adminSet(index)"><i class="fa fa-cog"></i></a>
                </td>
            </tr>
            </template>
            </tbody>
        </table>
    </div>
</div>
{/block}
{block name="js"}
<script>
    (()=>{
        var app = new Vue({
            el:'#container',
            data:{
                list:[],//管理员列表
                navList:[],//管理组列表
                thisNav:0,//记住点击的是第几个
            }
        });

        //删除管理员
        app.de = (i)=>{
            layer.confirm('确定要删除',{icon:3},function () {
                $.post(DUSER+'/delete_admin',{admin:app.list[i].id},function (da) {
                    if (da == 1) {
                        layer.msg('删除成功',{icon:1,offset:'rb'});
                    }
                    else{
                        layer.msg('删除失败',{icon:2,offset:'rb'});
                    }
                })
                app.list.splice(i,1);
                layer.closeAll();
            })
        };
        //修改密码 会员坐标
        app.updatePaw = (i)=>{
            layer.prompt({formType:1,title:'请输入新密码'},function (val) {
                $.post(DUSER+'/update_paw',{id:app.list[i].id,paw:val},function (da) {
                    if (da == 1) {
                        layer.msg('修改成功',{icon:1})
                    }else{
                        layer.msg('修改失败',{icon:1})
                    }
                })
                layer.closeAll();
            })
        }
        //设置权限 会员坐标
        app.adminSet = (i)=>{
            layer.open({
                type:2,
                title:'设置权限',
                offset:[0,'20px'],
                area:['400px','100%'],
                content:DUSER+'/admin_set/id/'+app.list[i].id
            })
        }
        //获取会员 i=会员组坐标
        app.upda = (i)=>{
            app.thisNav = i;
            getData(app.navList[app.thisNav].id,function (da) {
                app.list = da;
            });
        }
        //添加数据
        app.addData = ()=>{
            yeMian(DUSER+'/add_admin',function () {
                getData('group',function (da) {
                    app.navList = da;
                    //初始化会员
                    getData(app.navList[app.thisNav].id,function (da) {
                        app.list = da;
                    });
                },{area:['400px','400px']})
            });
        }

        //删除管理组
        app.deleteGroup = (i)=>{
            layer.confirm('确定要删除',{icon:3},function () {

                $.post(DUSER+'/delete_admin',{group:app.navList[i].id},function (da) {
                    console.log(da);
                    if(da == 1){
                        layer.msg('删除成功',{icon:1,offset:'rb'})
                    }
                    else{
                        layer.msg('删除成功',{icon:1,offset:'rb',end:function () {
                                location.reload();
                            }})
                    }
                })
                //删除页面数据
                app.navList.splice(i,1);
                /*该变坐标位置*/
                if(i < app.thisNav){
                    app.thisNav--;
                }
                layer.closeAll();
            })
        };
        //获取数据
        function getData($name,fn = 0) {
            $.post(DUSER+'/index',{name:$name},function (da) {
                fn(da);
            });
        }

        //初始管理组
        getData('group',function (da) {
            app.navList = da;
            //初始化会员
            getData(app.navList[app.thisNav].id,function (da) {
                app.list = da;
            });
        })


    })();
</script>
{/block}